const app = getApp()
import http from '../../utils/http'
Page({
    data: {
        // 基础参数
        roleId: 0,

        // 下拉选择
        selectSex: ['男生', '女生'],
        selectWish: ['减脂', '增肌', '均衡营养'],
        selectSport: ['轻量级活动', '中等强度活动', '高强度活动'],

        showSportDescription: false,

        pushFoodInfo: null,
        todayOrderInfos: [],
        orders: [],

        // 个人身体数据
        height: undefined,
        weight: undefined,
        sex: -1,
        wish: -1,
        sport: -1,
    },

    onLoad() {
        // 设置定时刷新
        var that = this
        var interval = setInterval(function () {
            if (that.data.roleId == 3) {
                that.listTodayOrders()
            }
        }, 3000)

    },

    onShow: function (options) {
        // 判断是否登录已成功
        if (app.globalData.sessionId) {
            this.setData({
                roleId: app.globalData.roleId
            })
            if (this.data.roleId == 0) {
                return;
            }
            if (this.data.roleId == 1) {
                this.getMyTodayOrder()
                this.listMyEndOrders()
            }
            if (this.data.roleId == 3) {
                this.listTodayOrders()
            }
        } else {
            app.userLoginCallback = () => {
                if (app.globalData.sessionId) {
                    this.setData({
                        roleId: app.globalData.roleId
                    })
                    if (this.data.roleId == 0) {
                        return;
                    }
                    if (this.data.roleId == 1) {
                        this.getMyTodayOrder()
                        this.listMyEndOrders()
                    }
                    if (this.data.roleId == 3) {
                        this.listTodayOrders()
                    }
                }
            };
        }
    },

    // 订单反馈
    questionOrder: function (e) {
        wx.redirectTo({
            url: '/pages/me_inner/feedback/feedback?orderId=' + e.currentTarget.dataset.item.id
        })
    },

    getFoodPush() {
        http({
            url: '/food/todayPush',
            success: (result) => {
                console.log(result)
                this.setData({
                    pushFoodInfo: result.info
                })
            }
        })
    },

    // 请求信息授权
    onTabItemTap() {
        if (app.globalData.roleId == 0) {

        } else {
            wx.requestSubscribeMessage({
                tmplIds: ['CfebW0YTdzFe7LPfV2MS39LHmVr0bvuRttgOzqhjHlE'],
                success(res) {
                    console.log(res);
                },
                fail(res) {
                    console.log(res);
                }
            })
        }
    },

    addOrder() {
        var that = this
        // 检查是否开放下单
        http({
            url: '/order/checkAble/'+that.data.pushFoodInfo.id,
            success: (result) => {
                console.log(result)
                if(result.bol){
                    wx.login({
                        success: res => {
                            if (res.code) {
                                http({
                                    // url: '/wxPay/' + 1 + '/' + res.code,
                                    url: '/wxPay/' + that.data.pushFoodInfo.sellingPrice + '/' + res.code,
                                    success: (result) => {
                                        var payResponse = result.payResponse;
                                        wx.requestPayment({
                                            timeStamp: payResponse.timeStamp,
                                            nonceStr: payResponse.nonceStr,
                                            package: payResponse.package,
                                            signType: payResponse.signType,
                                            paySign: payResponse.paySign,
                                            success() {
                                                // 微信支付成功,发起订单创建
                                                http({
                                                    url: '/order/create/' + that.data.pushFoodInfo.id,
                                                    success: (result) => {
                                                        wx.showToast({
                                                            title: '下单成功',
                                                            icon: '',
                                                            duration: 1000,
                                                        })
                                                        that.getMyTodayOrder()
                                                        wx.requestSubscribeMessage({
                                                            tmplIds: ['CfebW0YTdzFe7LPfV2MS39LHmVr0bvuRttgOzqhjHlE'],
                                                            success(res) {
                                                                console.log(res);
                                                            },
                                                            fail(res) {
                                                                console.log(res);
                                                            }
                                                        })
                                                    },
                                                    fail: function (e) {
                                                        console.log(e)
                                                        wx.showModal({
                                                            title: '提示',
                                                            content: '下单失败，请联系客服',
                                                        })
                                                    }
                                                })
                                            },
                                            fail: function (e) {
                                                console.log(e)
                                                if (e.errMsg != "requestPayment:fail cancel") {
                                                    wx.showModal({
                                                        title: '提示',
                                                        content: '下单失败，请联系客服',
                                                    })
                                                }
                                            }
                                        })
                                    }
                                })
                            } else {
                                wx.hideLoading()
                                wx.showModal({
                                    title: '提示',
                                    content: '服务器连接失败，请检查网络',
                                })
                            }
                        },
                        fail: function () {
                            wx.hideLoading()
                            wx.showModal({
                                title: '提示',
                                content: '服务器连接失败，请检查网络',
                            })
                        }
                    })
                }else{
                    wx.showModal({
                        title: '提示',
                        content: '本餐已卖完，下次记得早点哦~',
                    })
                }
            },
            fail: function (e) {
                console.log(e)
                wx.showModal({
                    title: '提示',
                    content: '下单失败，请联系客服',
                })
            }
        })
    },

    endOrder(e) {
        http({
            url: '/order/end/' + e.currentTarget.dataset.id,
            success: (result) => {
                this.listTodayOrders()
            }
        })
    },

    getMyTodayOrder() {
        var that = this
        http({
            url: '/order/today',
            success: (result) => {
                console.log(result)
                if (result.rows.length == 0) {
                    that.getFoodPush()
                    this.setData({
                        todayOrderInfos: null,
                    })
                } else {
                    this.setData({
                        todayOrderInfos: result.rows,
                        pushFoodInfo: null
                    })
                }
            }
        })
    },

    /**
     * 列出用户历史订单
     */
    listMyEndOrders() {
        http({
            url: '/order/oldOrders',
            success: (result) => {
                console.log(result)
                this.setData({
                    orders: result.rows
                })
            }
        })
    },

    listTodayOrders() {
        http({
            url: '/order/todayOrders',
            success: (result) => {
                console.log(result)
                this.setData({
                    orders: result.rows
                })
            }
        })
    },

    showSprotDescription() {
        this.setData({
            showSportDescription: true
        })
    },

    // 用户点击右上角分享
    onShareAppMessage: function () {
        const promise = new Promise(resolve => {
            setTimeout(() => {
                resolve({
                    title: '知食 young',
                    imageUrl: "/static/images/logo.png",
                })
            }, 500)
        })
        return {
            title: '知食 young',
            path: "/pages/order/order",
            imageUrl: "/static/images/logo.png",
            promise
        }
    },

    toDetail(e) {
        wx.navigateTo({
            url: '/pages/food_inner/detail/detail?id=' + e.currentTarget.dataset.id
        })
    },

    bindInput() {}
})